# Archlive <http://archlive-pkg.googlecode.com>
#
###################################################################
# To use this PKGBUILD:
# Firstly:  makepkg -g >>./PKGBUILD
# Then:   time makepkg 2>&1 | tee ./log
#          Use this command can output all the log to the file ./log
###################################################################

####################### Custom options#################
kver_file=kernel26-snapshot.kver
preset_file=kernel26-snapshot.preset
kernel_name=vmlinuz26-snapshot
default_img=kernel26-snapshot.img
fallback_img=kernel26-snapshot-fallback.img

install_file=kernel26-snapshot.install
[ "${install_file}" != "kernel26.install" ] && cp -f $startdir/kernel26.install $startdir/${install_file}

local_version="-ARCH"
_configure_as_ARCH_way=y
# The "EXTRAVERSION" will be removed if you select _configure_as_ARCH_way as "y". 
# The final kernel version (can get it with "uname -r") as "2.6.31-ARCH"

# If you need re-configure before compile
menuconfig="n"
_include_aufs2=y
_include_squashfs=y
_with_git_patch=y
_with_next_patch=n
_with_bfs_patch=n

# Git patch
#_git_patch_url=$(links -source http://www.kernel.org/ | grep "snapshots/patch-2.6.31-.*bz2" | grep -v "?" | awk -F "\"" '{print $2}')
_git_patch_url="http://www.kernel.org/pub/linux/kernel//v2.6/snapshots/patch-2.6.31-git15.bz2"
_git_patch=$(basename ${_git_patch_url} .bz2)

# Next patch
#_next_patch_url=$(links -source http://www.kernel.org/ | grep "patch-.*next.*bz2" | grep -v "?" | awk -F "\"" '{print $2}')
_next_patch_url="http://www.kernel.org/pub/linux/kernel/v2.6/next/patch-v2.6.31-next-20090924.bz2"
_next_patch=$(basename ${_next_patch_url} .bz2)

# Base patch
_basever=2.6.31
#_base_patch_url=$(links -source http://www.kernel.org/ | grep "patch-${_basever}.[0-9].*.bz2" | grep -v "?" | awk -F "\"" '{print $2}')
_base_patch_url="http://www.kernel.org/pub/linux/kernel/v2.6/patch-2.6.31.1.bz2"
_base_patch=$(basename ${_base_patch_url} .bz2)
######################################################

pkgname=('kernel26-snapshot')
#pkgname=('kernel26-snapshot' 'kernel26-firmware')
pkgver=2.6.31
pkgrel=1
if [ "x${_base_patch_url}" != "x" ]; then pkgver="${_base_patch##*-}"; fi
if [ "${_with_git_patch}" = "y" ]; then pkgver="${pkgver}_${_git_patch##*-}"; fi
if [ "${_with_next_patch}" = "y" ]; then pkgver="${pkgver}_next_${_next_patch##*-}"; fi
url="http://www.kernel.org/"
arch=(i686 x86_64)
pkgdesc="The Linux Kernel and modules with Gentoo patches, aufs2 file system patch, \
	and kernel Chinese characters patches. "
provides=('kernel26=2.6.31' 'aufs')
conflicts=('kernel24' 'kernel24-scsi' 'kernel26-scsi' 'kernel26' 'unionfs' 'aufs')
depends=('coreutils' 'module-init-tools' 'mkinitcpio>=0.5.15')
if [ "${preset_file}" = "kernel26.preset" ]; then
	backup=(etc/mkinitcpio.d/kernel26.preset)
fi
install=${install_file}
license=('GPL2')
groups=('base')
makedepends=('gcc' 'git' 'links' 'wget')

source=(config.{i686,x86_64} logo_linux_{clut224.ppm,mono.pbm,vga16.ppm}
	http://www.cn.kernel.org/pub/linux/kernel/v2.6/linux-$_basever.tar.bz2

	## reiserfs4 patch
	#http://www.kernel.org/pub/linux/kernel/people/edward/reiser4/reiser4-for-2.6/reiser4-for-${_basever}.patch.gz

	## Gentoo patches
	http://sources.gentoo.org/viewcvs.py/*checkout*/linux-patches/genpatches-2.6/trunk/2.6.31/4100_dm-bbr.patch
	http://sources.gentoo.org/viewcvs.py/*checkout*/linux-patches/genpatches-2.6/trunk/2.6.31/4202_fbcondecor-0.9.6.patch
	http://sources.gentoo.org/viewcvs.py/*checkout*/linux-patches/genpatches-2.6/trunk/2.6.31/4400_alpha-sysctl-uac.patch

	### Chinese Characters Patches http://youbest.cublog.cn/
	http://zdbr.net.cn/download/utf8-kernel-2.6.31-core-1.patch.bz2
	http://zdbr.net.cn/download/utf8-kernel-2.6-fonts-2.patch.bz2
	http://zdbr.net.cn/download/utf8-kernel-2.6.28-fbcondecor-1.patch.bz2

	# RealTime patch
	#ftp://ftp.kernel.org/pub/linux/kernel/projects/rt/patch-2.6.31-rt10.bz2

	#BFS patches
	bfs1_2.6.31-sched-bfs-232.patch::"http://ck.kolivas.org/patches/bfs/2.6.31-sched-bfs-232.patch"
	bfs2_bfs230-fixdodgydrivers2.patch::"http://ck.kolivas.org/patches/bfs/bfs230-fixdodgydrivers2.patch"
	bfs3_autoiso-xorg.patch::"http://ck.kolivas.org/patches/bfs/autoiso-xorg.patch"

	# Fedora KMS patch for nouveau
	#f072_drm-next.patch::"http://cvs.fedoraproject.org/viewvc/rpms/kernel/F-12/drm-next.patch?view=co"
	f073_drm-nouveau.patch::"http://cvs.fedoraproject.org/viewvc/rpms/kernel/F-12/drm-nouveau.patch?view=co"
)

build() {

	KARCH=x86
	cd "$srcdir/linux-$_basever"

	###########################################
	###	Apply patches				##
	###########################################

	msg "Add patches..."

	msg2 "Adding Archlinux logo..."
	cp "$srcdir/logo_linux_clut224.ppm"  drivers/video/logo/ &&
	cp "$srcdir/logo_linux_mono.pbm"     drivers/video/logo/ &&
	cp "$srcdir/logo_linux_vga16.ppm"    drivers/video/logo/ || return 1

	if [ "x${_base_patch_url}" != "x" ]; then
		msg2 "Add base patch ${_base_patch}"
		wget ${_base_patch_url}
		bzip2 -d  ${_base_patch}.bz2
		patch -Np1 -i ${_base_patch} || return 1
	fi

	if [ "x${_with_git_patch}" = "y" ]; then
		msg2 "Add git patch ${_git_patch} ..."
		wget ${_git_patch_url}
		bzip2 -d  ${_git_patch}.bz2
		patch -Np1 -i ${_git_patch}
	fi
	if [ "${_with_next_patch}" = "y" ]; then
		msg2 "Add next patch ${_next_patch} ..."
		wget ${_next_patch_url}
		bzip2 -d  ${_next_patch}.bz2
		patch -Np1 -i ${_next_patch}
	fi

	msg2 "Add Gentoo patches ..."
	for i in $(ls $srcdir/4[0-9][0-9][0-9]*);do
    		echo "Applying $(basename $i)" 
  		patch -Np1 -i $i || return 1
	done

	msg2 "Add the kernel Chinese characters patches http://youbest.cublog.cn/"
	patch -Np1 -i $srcdir/utf8-kernel-2.6.31-core-1.patch || return 1
	patch -Np1 -i $srcdir/utf8-kernel-2.6.28-fbcondecor-1.patch || return 1
	patch -Np1 -i $srcdir/utf8-kernel-2.6-fonts-2.patch || return 1

#	msg2 "Adding reiserfs4 patch reiser4-for-${_basever}.patch"
#	patch -Np1 -i $srcdir/reiser4-for-${_basever}.patch || return 1

	if [ "${_include_aufs2}" = "y" ]; then 
		msg2 "Fetch the aufs2-standalone git tree..."
		if [ -d $startdir/aufs2-standalone ]; then
			cd $startdir/aufs2-standalone
			git pull origin
		else
			git clone http://git.c3sl.ufpr.br/pub/scm/aufs/aufs2-standalone.git $startdir/aufs2-standalone || return 1
		fi
		[ -d $srcdir/aufs2-standalone ] && rm -rf $srcdir/aufs2-standalone
		cp -a $startdir/aufs2-standalone $srcdir/aufs2-standalone
		pushd $srcdir/aufs2-standalone
		git checkout origin/aufs2
		popd
		cp -av $srcdir/aufs2-standalone/{Documentation,fs,include,aufs2-base.patch,aufs2-kbuild.patch,aufs2-standalone.patch} $srcdir/linux-$_basever/
		cd $srcdir/linux-$_basever
		msg2 "Adding aufs2 patches..."
		patch -Np1 -i aufs2-kbuild.patch #|| return 1
		patch -Np1 -i aufs2-base.patch #|| return 1
		patch -Np1 -i aufs2-standalone.patch #|| return 1
	fi
#	msg2 "Apply RealTime patches..."
#	patch -Np1 -i $srcdir/patch-2.6.31-rt10 #|| return 1

	if [ "${_with_bfs_patch}" = "y" ]; then
		msg2 "Apply BFS patches..."
		for i in $(ls $srcdir/bfs[1-9]*);do
			msg "Applying ${i##*/}..."
			patch -Np1 -i $i
		done
	fi

	msg2 "Add Fedora patches ..."
	for i in $(ls $srcdir/f[0-9][0-9][0-9]*);do
 		echo "Applying $(basename $i)" 
		patch -Np1 -i $i #||return 1
	done

	###########################################
	###	Configuration				##
	###########################################


	if [ "$CARCH" = "x86_64" ]; then
		cat ../config.x86_64 >./.config
	else
		cat ../config.i686 >./.config
	fi

	if [ "$_configure_as_ARCH_way" = "y" ]; then
		msg "Configure the kernel version as ARCH way, like 2.6.31-ARCH"
		echo "" >localversion-next
		sed -i "s|^EXTRAVERSION = .*$|EXTRAVERSION =|g" Makefile
		sed -i "s|.*CONFIG_LOCALVERSION=.*|CONFIG_LOCALVERSION=\"${local_version}\"|g" .config
		sed -i "s|.*CONFIG_LOCALVERSION_AUTO.*|\# CONFIG_LOCALVERSION_AUTO is not set|g" .config
	fi

	# The default .config without support for squashfs, can change it by set "_include_squashfs" as "y".
	if [ "$_include_squashfs" = "y" ]; then
		msg "Selected squashfs4 included in kernel package."
		if [ "x$(grep "CONFIG_SQUASHFS=m" .config)" = "x" ]; then
			sed -i "s|.*CONFIG_SQUASHFS.*|CONFIG_SQUASHFS=m\
				\n# CONFIG_SQUASHFS_EMBEDDED is not set\
				\nCONFIG_SQUASHFS_FRAGMENT_CACHE_SIZE=3|g" ./.config
		fi
	else
		if [ "x$(grep "CONFIG_SQUASHFS=m" .config)" != "x" ]; then
			sed -i "s|CONFIG_SQUASHFS=m|\# CONFIG_SQUASHFS is not set|g" .config
		fi
	fi
	if [ "${_include_aufs2}" = "y" ]; then 
		msg "Selected aufs2 included in kernel package."
		if [ "x$(grep "CONFIG_AUFS_FS=m" .config)" = "x" ]; then
			sed -i "/.*CONFIG_NILFS2_FS.*/ a\
				\CONFIG_AUFS_FS=m\
				\n# CONFIG_AUFS_BRANCH_MAX_127 is not set\
				\n# CONFIG_AUFS_BRANCH_MAX_511 is not set\
				\nCONFIG_AUFS_BRANCH_MAX_1023=y\
				\n# CONFIG_AUFS_BRANCH_MAX_32767 is not set\
				\nCONFIG_AUFS_HINOTIFY=y\
				\nCONFIG_AUFS_EXPORT=y\
				\nCONFIG_AUFS_RDU=y\
				\nCONFIG_AUFS_SHWH=y\
				\nCONFIG_AUFS_BR_RAMFS=y\
				\nCONFIG_AUFS_BR_FUSE=y\
				\n# CONFIG_AUFS_DEBUG is not set\
				\nCONFIG_AUFS_BDEV_LOOP=y\
				\nCONFIG_AUFS_POLL=y" ./.config
		fi
	else
		if [ "x$(grep "CONFIG_AUFS_FS=m" .config)" != "x" ]; then
			sed -i "s|CONFIG_AUFS_FS=m|\# CONFIG_AUFS_FS is not set|g" .config
		fi
	fi
	make prepare
	msg "Update .config Finished"

	if [ "$menuconfig" = "1" ]; then
		make menuconfig
	fi
	yes "" | make config

	# STARTING BUILDING
	msg "Checking the kernel version..."
	_kernver="$(make kernelrelease)"
	if [ "$_configure_as_ARCH_way" = "y" ]; then
		# Check the _kernver again, if it is your wanted (Special for _configure_as_ARCH_way)
		_localversion=$(grep CONFIG_LOCALVERSION= .config | cut -d "\"" -f 2)
		if [ "${_kernver#*-}" != ${_localversion#-} ]; then
			msg "There are some errors when get your own kernel version."
			msg "_kernver=${_kernver}, _localversion=${_localversion}."
			exit 1
		else
			msg "Get the right kernel version as ${_kernver}"
		fi
	fi

	###########################################
	###	Compile					##
	###########################################

	msg "Building kernel and modules..."
	make bzImage modules || return 1
	msg "Compile finished!"

	msg "Start to install..."

	cd "$srcdir/linux-$_basever"
	_kernver="$(make kernelrelease)"
	###########################################
	###	Install					##
	###########################################
	mkdir -p ${pkgdir}/{lib/modules,boot}

	msg2 "Install kernel modules"
	make INSTALL_MOD_PATH=${pkgdir} modules_install || return 1

	msg2 "Install kernel"
	cp System.map ${pkgdir}/boot/System.map26${_kernelname}
	cp arch/$KARCH/boot/bzImage ${pkgdir}/boot/vmlinuz26${_kernelname}

	msg2 "Copy kernel source"
	install -D -m644 Makefile \
		${pkgdir}/usr/src/linux-${_kernver}/Makefile
	install -D -m644 kernel/Makefile \
		${pkgdir}/usr/src/linux-${_kernver}/kernel/Makefile
	install -D -m644 .config \
		${pkgdir}/usr/src/linux-${_kernver}/.config
	mkdir -p ${pkgdir}/usr/src/linux-${_kernver}/include

	for i in acpi asm-{generic,x86} config linux math-emu media net pcmcia scsi sound trace video; do
		cp -a include/$i ${pkgdir}/usr/src/linux-${_kernver}/include/
	done

	# copy arch includes for external modules
	mkdir -p ${pkgdir}/usr/src/linux-${_kernver}/arch/x86
	cp -a arch/x86/include ${pkgdir}/usr/src/linux-${_kernver}/arch/x86/

	# copy files necessary for later builds, like nvidia and vmware
	cp Module.symvers ${pkgdir}/usr/src/linux-${_kernver}
	cp -a scripts ${pkgdir}/usr/src/linux-${_kernver}
	# fix permissions on scripts dir
	chmod og-w -R ${pkgdir}/usr/src/linux-${_kernver}/scripts
	#mkdir -p ${pkgdir}/usr/src/linux-${_kernver}/.tmp_versions

	mkdir -p ${pkgdir}/usr/src/linux-${_kernver}/arch/$KARCH/kernel

	cp arch/$KARCH/Makefile ${pkgdir}/usr/src/linux-${_kernver}/arch/$KARCH/
	if [ "$CARCH" = "i686" ]; then
	cp arch/$KARCH/Makefile_32.cpu ${pkgdir}/usr/src/linux-${_kernver}/arch/$KARCH/
	fi
	cp arch/$KARCH/kernel/asm-offsets.s ${pkgdir}/usr/src/linux-${_kernver}/arch/$KARCH/kernel/

	# add headers for lirc package
	mkdir -p ${pkgdir}/usr/src/linux-${_kernver}/drivers/media/video
	cp drivers/media/video/*.h  ${pkgdir}/usr/src/linux-${_kernver}/drivers/media/video/
	for i in bt8xx cpia2 cx25840 cx88 em28xx et61x251 pwc saa7134 sn9c102 usbvideo zc0301; do
		mkdir -p ${pkgdir}/usr/src/linux-${_kernver}/drivers/media/video/$i
		cp -a drivers/media/video/$i/*.h ${pkgdir}/usr/src/linux-${_kernver}/drivers/media/video/$i
	done

	# add docbook makefile
	install -D -m644 Documentation/DocBook/Makefile \
		${pkgdir}/usr/src/linux-${_kernver}/Documentation/DocBook/Makefile

	# add dm headers
	mkdir -p ${pkgdir}/usr/src/linux-${_kernver}/drivers/md
	cp drivers/md/*.h  ${pkgdir}/usr/src/linux-${_kernver}/drivers/md

	# add inotify.h
	mkdir -p ${pkgdir}/usr/src/linux-${_kernver}/include/linux
	cp include/linux/inotify.h ${pkgdir}/usr/src/linux-${_kernver}/include/linux/

	# add CLUSTERIP file for iptables
	mkdir -p $startdir/pkg/usr/src/linux-$_kernver/net/ipv4/netfilter/
	cp net/ipv4/netfilter/ipt_CLUSTERIP.c $startdir/pkg/usr/src/linux-$_kernver/net/ipv4/netfilter/

	# add wireless headers
	mkdir -p ${pkgdir}/usr/src/linux-${_kernver}/net/mac80211/
	cp net/mac80211/*.h ${pkgdir}/usr/src/linux-${_kernver}/net/mac80211/

	# add dvb headers for external modules
	# in reference to:
	# http://bugs.archlinux.org/task/9912
	mkdir -p ${pkgdir}/usr/src/linux-${_kernver}/drivers/media/dvb/dvb-core
	cp drivers/media/dvb/dvb-core/*.h ${pkgdir}/usr/src/linux-${_kernver}/drivers/media/dvb/dvb-core/

	# add dvb headers for http://mcentral.de/hg/~mrec/em28xx-new
	# in reference to:
	# http://bugs.archlinux.org/task/13146
	mkdir -p ${pkgdir}/usr/src/linux-${_kernver}/drivers/media/dvb/frontends/
	cp drivers/media/dvb/frontends/lgdt330x.h ${pkgdir}/usr/src/linux-${_kernver}/drivers/media/dvb/frontends/
	cp drivers/media/video/msp3400-driver.h ${pkgdir}/usr/src/linux-${_kernver}/drivers/media/dvb/frontends/

	# add xfs and shmem for aufs building
	mkdir -p ${pkgdir}/usr/src/linux-${_kernver}/fs/xfs
	mkdir -p ${pkgdir}/usr/src/linux-${_kernver}/mm
	cp fs/xfs/xfs_sb.h ${pkgdir}/usr/src/linux-${_kernver}/fs/xfs/xfs_sb.h
	cp mm/shmem.c $startdir/pkg/usr/src/linux-$_kernver/mm/shmem.c

	# add headers vor virtualbox
	# in reference to:
	# http://bugs.archlinux.org/task/14568
	cp -a include/drm $pkgdir/usr/src/linux-${_kernver}/include/
	# add headers for broadcom wl
	# in reference to:
	# http://bugs.archlinux.org/task/14568
	cp -a include/trace $pkgdir/usr/src/linux-${_kernver}/include/

	# add vmlinux
	cp vmlinux ${pkgdir}/usr/src/linux-${_kernver}

	# copy in Kconfig files
	for i in `find . -name "Kconfig*"`; do 
		mkdir -p ${pkgdir}/usr/src/linux-${_kernver}/`echo $i | sed 's|/Kconfig.*||'`
		cp $i ${pkgdir}/usr/src/linux-${_kernver}/$i
	done

	cd ${pkgdir}/usr/src/linux-${_kernver}/include && ln -s asm-$KARCH asm

	chown -R root.root ${pkgdir}/usr/src/linux-${_kernver}
	find ${pkgdir}/usr/src/linux-${_kernver} -type d -exec chmod 755 {} \;
	cd ${pkgdir}/lib/modules/${_kernver} && \
		(rm -f source build; ln -sf ../../../usr/src/linux-${_kernver} build)


	mkdir -p $pkgdir/etc/mkinitcpio.d

	msg "Generating kernel26.kver..."
	echo -e "# DO NOT EDIT THIS FILE\nALL_kver='$_kernver'" \
		> "$pkgdir/etc/mkinitcpio.d/${kver_file}" || return 1

	msg "Generating preset file..."
	echo -e "# mkinitcpio preset file for $pkgname\n \
		\n########################################\
		\n# DO NOT EDIT THIS LINE:\
		\nsource /etc/mkinitcpio.d/${kver_file}\
		\n########################################\
		\nALL_config=\"/etc/mkinitcpio.conf\"\
		\n\nPRESETS=('default' 'fallback')\
		\n\n#default_config=\"/etc/mkinitcpio.conf\" \
		\ndefault_image=\"/boot/${default_img}\" \
		\n#default_options=\"\" \
		\n#fallback_config=\"/etc/mkinitcpio.conf\" \
		\nfallback_image=\"/boot/${fallback_img}\" \
		\nfallback_options=\"-S autodetect\" "\
		> "$pkgdir/etc/mkinitcpio.d/${preset_file}" || return 1

	msg "Update install scripts..."
	sed -i "s%kernelname=.*%kernelname=${kernel_name}%g;\
		s%default_img=.*%default_img=${default_img}%g;\
		s%fallback_img=.*%fallback_img=${fallback_img}%g;\
		s%pacman.*grep%pacman -Ql $pkgname | grep%g;\
		s%mkinitcpio.*%mkinitcpio -p ${preset_file%.preset}%g;\
		s%kernelname=.*%kernelname="${kernel_name}"%g"\
		$startdir/${install_file} || return 1

	msg2 "remove unneeded architectures"
	rm -rf ${pkgdir}/usr/src/linux-${_kernver}/arch/{alpha,arm,arm26,avr32,blackfin,cris,frv,h8300,ia64,m32r,m68k,m68knommu,mips,microblaze,mn10300,parisc,powerpc,ppc,s390,sh,sh64,sparc,sparc64,um,v850,xtensa}

	if [ "${_include_firmware}" != "y" ]; then
		msg2 "remove the firmware"
		rm -rf ${pkgdir}/lib/firmware
	fi
}